<?php
require("gConfig.php");
require("des.php");
// $uid = $_POST['uid'];
// if(!isset($uid)){
// 	exit();
// }
// checkuid($uid, 0);
$param = $_POST['param'];
// $param = $_GET['param'];
$desparams = getdesparam($param);
$uid = $desparams['uid'];
if(!isset($uid)){
	exit();
}
checkuid($uid, 0);
$type = $desparams['type'];
$room = $desparams['room'];
$sRound = $desparams['sRound'];
$P = $desparams['oP'];
$B = $desparams['oB'];
$PB = $desparams['oPB'];
// $type = $_GET['type'];
// $uid = $_GET['uid'];
// $room = $_GET['room'];
// $sRound = $_GET['sRound'];
// $P = $_GET['oP'];
// $B = $_GET['oB'];
// $PB = $_GET['oPB'];
// $gamestr = $_GET['games'];

//$room = $_POST['room'];
//$type = $_POST['type'];
if ($type == '0'){
	$sqlstr = "select * from m_state where sId='$room' limit 1 ";
	$result = mysql_query($sqlstr,$comm);
	if(1<= mysql_num_rows($result)){
		$row = mysql_fetch_row($result);
		if($row[1] == 10){
			$row[2] = (int)$row[2] + 1;
			echo "$row[0]|$row[2]|10";
		}
		echo "|||";
	}else{
		echo "|||";
}
}else if($type == '1'){
	$sqlstr = "update m_state set sState=1,sRound='$sRound',sHis='$P@$B@$PB|$gamestr' where sId='$room' limit 1 ";
	$result = mysql_query($sqlstr,$comm);
	echo "$room|$sRound|1";
}else if($type == '2'){
	$sqlstr = "update m_state set sState=2,sHis='$P@$B@$PB|$gamestr' where sId='$room' and sRound='$sRound' limit 1 ";
	$result = mysql_query($sqlstr,$comm);
	echo "$room|$sRound|2";
}
else if($type == '3'){
	$sqlstr = "select sState from m_state where sId='$room' and sRound='$sRound' ";
	$result = mysql_query($sqlstr,$comm);
	$row = mysql_fetch_row($result);
	if($row[0] != 2){
		echo "$room|$sRound|$row[0]";
		exit();
	}
	//得到结果并计算
	
	//更新房间状态
	$sqlstr = "update m_state set sState=3,sHis='$P@$B@$PB|$gamestr' where sId='$room' and sRound='$sRound' limit 1 ";
	$result = mysql_query($sqlstr,$comm);
	//获取下注情况以计算输赢
	//计算房间状态
	$sqlstr = "select sBets from m_state where sId='$room' and sRound='$sRound' limit 1 ";
	$result = mysql_query($sqlstr,$comm);
	$row = mysql_fetch_row($result);
	$sBets = explode(',', $row[0]);
	$remark = culresult();
	$in = culin($remark,$sBets);
	$out = culout($remark,$sBets);
	$win = cultwin($remark,$sBets);
	$sqlstr = "insert into b_out(oDate,oRoom,oRound,oP,oB,oPB,oResult,oIn,oOut,oBanker)  values (current_timestamp(),$room,$sRound,'$P','$B','$PB',$remark,$out,$win,'') ";
	$result = mysql_query($sqlstr,$comm);
	
	if( substr( $CFG['Check'], 20, 1 ) == "1"){
		//计算退水
	}
	
	//计算所有下注成员
	$sqlstr = "select *  from m_users where uId=$room ";
	$result = mysql_query($sqlstr,$comm);
	while ($row = mysql_fetch_row($result)){
		$Groups = explode('@', $row[1]);
		$name = $row[2];
		$Bets = explode(',', $row[3]);
		$win = $row[4];
//		$betall = $row[5];
		$Credit = $row[6];
		$betall = 0;
		$betall = $Bets[0] + $Bets[1] + $Bets[2] + $Bets[3] + $Bets[4] + $Bets[5] + $Bets[6];
		$setBet = $Bets[0] + $Bets[1] + $Bets[2] + $Bets[3] + $Bets[4] + $Bets[5] + $Bets[6];
		$in = culin($remark,$Bets);
		if($betall >0 ){
			insertbtran($name,$Groups,$Bets,$Credit,$setBet,$remark);
		}
	}
	
	echo "$room|$sRound|3";
}else if($type == 10){
	//更新房间状态
	$sqlstr = "update m_state set sState=10,sBets='0,0,0,0,0,0,0' where sId='$room' and sRound='$sRound' limit 1 ";
	$result = mysql_query($sqlstr,$comm);
	//更新用户状态
	$sqlstr = "select *  from m_users where uId=$room ";
	$result = mysql_query($sqlstr,$comm);
	while ($row = mysql_fetch_row($result)){
		$name = $row[2];
		$sqlstr = "update m_users set uBets='0,0,0,0,0,0,0' where uName='$name' ";
		mysql_query($sqlstr,$comm);
	}
	echo "$room|$sRound|10";
}
else{
//	culresult();
	//获取房间状态
	$sqlstr = "select sRound,sState from m_state where sId='$room' limit 1 ";
	$result = mysql_query($sqlstr,$comm);
	$row = mysql_fetch_row($result);
	echo "$room|$row[0]|$row[1]";
}

function insertbtran($name,$Groups,$Bets,$Credit,$setBet,$remark) {
	global  $room;
	global  $sRound;
	global $comm;
	global $CFG;
	
	$in = culin($remark,$Bets);
	$out = culout($remark, $Bets);
	$win = cultwin($remark,$Bets);
	
	$baseWater = culbaseWater($remark, $Bets);
	$tRang = culRang($name,$Groups);
	$water = cultWater($name,$Groups,$baseWater);
	$betstr = $Bets[0].",".$Bets[1].",".$Bets[2].",".$Bets[3].",".$Bets[4].",".$Bets[5].",".$Bets[6];
	
	$query = "select gState,gGid from b_group where gName ='$name' ";
	$result = mysql_query($query,$comm);
	$row = mysql_fetch_row($result);
	$gState = $row[0];
	$tState = 0;
	if(gState&8){
		$tState = $tState|1;
	}
	if(gState&32){
		$tState = $tState|32;
	}
	//更新 计算结果
	$query = "update b_group set gUse=gUse+$win-$out,gCredit=gCredit+$win-$out,gBet=gBet+$setBet,gBetWin=gBetWin+$in where gName='$name' ";
	mysql_query($query,$comm);
	//更新 muser
	$query = "select gCredit,gBetWin,gBet,gUse from b_group where gName='$name' ";
	$result = mysql_query($query,$comm);
	$row = mysql_fetch_row($result);
	$Credit = intval($row[3]);
	$query = "update m_users set uCredit='{$Credit}',uWin=$row[1],uBet=$row[2] where uName='$name' ";
	mysql_query($query,$comm);
	
	$query = "select gUse from b_group where gName='$name' ";
	$result = mysql_query($query,$comm);
	$row = mysql_fetch_row($result);
	$Score = $row[0];
	if(!isset($Score)) $Score = 0.0;	
	
	$query="insert into 
	b_tran(tState,tDate,
	tName,tName1,tName2,tName3,tName4,
	tScore,tRoom,tRound,tResult,
	tWin,tWater,tRang,
	tBet,tBetWin,tBets)
	values('$tState', current_timestamp(),
	'$name','$Groups[1]','$Groups[2]','$Groups[3]','$Groups[4]',
	$Score,$room,$sRound,$remark,
	$win,'$water','$tRang',
	$setBet,$in,'$betstr'
	)";
	mysql_query($query,$comm);
}

function cultWater($name,$Groups,$baseWater) {
	global $comm;
	
	$query = "select gGame1,gName,gLevel from b_group where gName in ('$name','$Groups[1]','$Groups[2]','$Groups[3]','$Groups[4]')";
	$result = mysql_query($query,$comm);
//	$row = mysql_fetch_row($result);
	$tWater = "";
	while($row = mysql_fetch_row($result)){
		$game1s = explode(",", $row[0]);
		$tWater .= $game1s[0];
		$Rang1 = intval(intval($baseWater)*floatval($game1s[0])/10);
		if ($row[2] == 5){
			$query = "update b_group set gRang1=gRang1+$Rang1 where gName='{$row[1]}'";
			mysql_query($query,$comm);
		}else{
// 			$query = "update b_group set gRang1=gRang1+$Rang1 where gName='{$row[1]}'";
// 			mysql_query($query,$comm);
		}
		
		$tWater .= ",";
	}
	$tWater = substr($tWater, 0,strlen($tWater)-1);
	return  $tWater;
}

function cultwin($remark,$Bets) {
	$culwin = 0;
	
	$a = $remark & 3;
	if($a == 3){
		$culwin += (int)$Bets[2] * (9-1);
	}
	if($a==1){
		$culwin += (int)$Bets[0] * (2-1);
	}
	if($a==2){
		$culwin += (int)$Bets[1] * (1.95-1);
	}
	
	if(($remark & 4)==4){
		$culwin += (int)$Bets[3] * (12-1);
	}
	if(($remark & 8)==8){
		$culwin += (int)$Bets[4] * (12-1);
	}
	
	return  $culwin;
}

function culresult() {
	global $P;
	global $B;
	global $PB;
	
	$result = 0;
	$cmpPB = explode(":", $PB);
	if($cmpPB[0] == $cmpPB[1]){//和
		$result = $result | 3;
	}
	if($cmpPB[0] > $cmpPB[1]){//闲
		$result = $result | 1;
	}else{
		$result = $result | 2;//庄
	}
	
	//出现三张以上
	if(strlen($P)>4 || strlen($B)>4)
	{
		$result = $result | 16;
	}
	//判断对
	$a="";$b="";$c="";
	if(strlen($P) == 4){
		$a = substr($P, 1,1);
		$b = substr($P, 3,1);
		if ($a == $b) {
			$result = $result | 4;
		}
	}
	if(strlen($P) == 6){
		$a = substr($P, 1,1);
		$b = substr($P, 3,1);
		$c = substr($P, 5,1);
		if ($a == $b) {
			$result = $result | 4;
		}
		if ($a == $c) {
			$result = $result | 4;
		}
		if ($b == $c) {
			$result = $result | 4;
		}
	}
	
	if(strlen($B) == 4){
		$a = substr($B, 1,1);
		$b = substr($B, 3,1);
		if ($a == $b) {
			$result = $result | 8;
		}
	}
	if(strlen($B) == 6){
		$a = substr($B, 1,1);
		$b = substr($B, 3,1);
		$c = substr($B, 5,1);
		if ($a == $b) {
			$result = $result | 8;
		}
		if ($a == $c) {
			$result = $result | 8;
		}
		if ($b == $c) {
			$result = $result | 8;
		}
	}
	return $result;
}

function culin($remark,$Bets){
	$in = 0;
	
	$a = $remark & 3;
	if($a == 3){//和
		$in += (int)$Bets[2];
	}
	if($a==1){//闲
		$in += (int)$Bets[0];
		$in += (int)$Bets[6];
	}
	if($a==2){//庄
		$in += (int)$Bets[1];
		$in += (int)$Bets[5];
	}

	
	if(($remark & 4)==4){//闲对
		$in += (int)$Bets[3];
	}
	if(($remark & 8)==8){//庄对
		$in += (int)$Bets[4];
	}
	
	return  $in;
}

function culout($remark,$Bets){
	for($i=0;$i<count($Bets);$i++){
    	$allbet += (int)$Bets[$i];
    }
    return ($allbet - culin($remark,$Bets));
}

function  culbaseWater($remark,$Bets){
	$reint = 0;
	$a = $remark & 3;
	if($a==1){
		$reint = intval($Bets[0]);
	}
	if($a==2){
		$reint = intval($Bets[1]);
	}
	return $reint;
}

function culRang($name,$Groups) {
	global $comm;
	global $CFG;
	$query = "select gGid,gIp,gGift from b_group where gName = '{$name}'";
	$result = mysql_query($query,$comm);
	$row = mysql_fetch_row($result);
	$gid = intval($row[0]);
	$ip = $row[1];
	$gift = $row[2];
	if(gid == 0){
		$rate = 1;
	}else{
		$rate = $CFG['CurrR'][$gid];
	}
	$query = "select gRang from b_group where gName in ('$Groups[1]','$Groups[2]','$Groups[3]','$Groups[4]')";
	$result = mysql_query($query,$comm);
	$reRang = "";
	while($row = mysql_fetch_row($result)){
		$reRang .= $row[0].",";
	}
	$query = "select gRang1 from b_group where gName = '$Groups[4]'";
	$result = mysql_query($query,$comm);
	$row = mysql_fetch_row($result);
	$reRang .= $row[0].",";
	$reRang .= $gid.",";
	$reRang .= $rate.",";
	$reRang .= "$ip,$gift";
}

function getdesparam($encodeStr) {
	global $desparams;
	$des = new DES("abcd1234");
	$str = $des->decrypt($encodeStr);
	if(!$str){
		echo "||";
		exit();
	}
	$params = explode("&", $str);
	foreach ($params as $str){
		$feil = explode("=", $str);
		if(count($feil)==2){
			$key = $feil[0];
			$value = $feil[1];
			$desparams[$key] = $value;
		}
	}
	return  $desparams;
}
?>